package com.sihan.swvp.action.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.entity.sys.Enterprise;
import com.sihan.swvp.entity.sys.User;
import com.sihan.swvp.service.sys.EnterpriseService;
import com.sihan.swvp.service.sys.LogService;


@Controller
@RequestMapping("/enterprise")
public class DeptAction extends BaseAction{
	
	@Autowired
	EnterpriseService enterpriseService;
	@Autowired
	public LogService logService;
	
	@ResponseBody
	@RequestMapping(value="/getEnterprise")
	public Object getEnterprise(@RequestParam String node){
		User user = getLoginUser();
		List<Map<String, Object>> list = enterpriseService.getEnterprise(node,user);
		for (Map<String, Object> map : list) {
			getCh(map, user); 
		}
		return list;
	} 
	
	@ResponseBody
	@RequestMapping(value="/getPrivEnterPrise")
	public Object getPrivEnterPrise(){
		List<Map<String,Object>> datas = enterpriseService.getPrivEnterPrise(getLoginUser());
		
		return datas;
	}
	
	private void getCh(Map<String, Object> map,User  user){
		if(true){
			List<Map<String,Object>> list = enterpriseService.getEnterprise(String.valueOf(map.get("id")),user);
			for (Map<String, Object> map2 : list) {
				getCh(map2,user);
			}
			map.put("children", list);
		}
	}
	
	
	@ResponseBody
	@RequestMapping(value="/addEnterprise")
	public Object addEnterprise(@ModelAttribute Enterprise enterprise){
		try {
			enterprise.setUpuser(getLoginUser().getId());
			Map<String, Object> param=new HashMap<String, Object>(); 
			param.put("name", enterprise.getName());
			param.put("pid", enterprise.getPid());
			param.put("id", enterprise.getId());
			Integer number=enterpriseService.getNameValidate(param);
			if(number !=0){
				return failMsg("此目录下，已存在该部门！");
			}
			Integer id = enterpriseService.addEnterprise(enterprise);
			logService.addUserLog("添加机构", "组织机构", getLoginUser().getId(), "添加机构："+enterprise.getName(),getRemoteAddr(),1,1);
			return successMsg(id);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logService.addUserLog("添加机构", "组织机构", getLoginUser().getId(), "添加机构："+enterprise.getName(),getRemoteAddr(),0,1);
			return failMsg("服务器异常");
		}
	} 
	
	@ResponseBody
	@RequestMapping(value="/deleteEnterprise")
	public Object deleteEnterprise(@RequestParam Integer id,String deptname){
		try {
			enterpriseService.deleteEnterprise(id,getLoginUser().getId());
			logService.addUserLog("删除机构", "组织机构", getLoginUser().getId(), "删除机构："+deptname,getRemoteAddr(),1,1);
			return successMsg("");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logService.addUserLog("删除机构", "组织机构", getLoginUser().getId(), "删除机构："+deptname,getRemoteAddr(),0,1);
			return failMsg("服务器异常");
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/companyIsSure")
	public int companyIsSure(@RequestParam String name){
		
		String eid = getParameter("eid");
		
		return enterpriseService.isCompany(eid,name);
	}
	
	@ResponseBody
	@RequestMapping(value="/departmentIsSure")
	public int departmentIsSure(@RequestParam String name){
		
		String eid = getParameter("eid");
		String id = getParameter("id");
		
		return enterpriseService.validDept(eid,id,name);
	}
	
}
